package com.example.springboot_project.controller;

import com.example.springboot_project.entity.Majors;
import com.example.springboot_project.entity.RespInfo;
import com.example.springboot_project.service.MajorsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/majors")
public class MajorsController {
    @Autowired
    private MajorsService majorsService;

    //查询所有学生信息
    @RequestMapping("findAll")
    public RespInfo findAll(){
//        List<Majors> list = MajorsService.findAll();
        List<Majors> l = MajorsService.findAll();
        System.out.println(l);
        //设置返回信息
        RespInfo respInfo = new RespInfo();
        respInfo.setCode(1);
        respInfo.setMsg("获取成功");
        respInfo.setData(l);
        return respInfo;
    }
    //添加
    @RequestMapping("addMajor")
    public RespInfo addMajor(Majors majors) {
        int a = majorsService.insert(majors);
        System.out.println(a);
        RespInfo respInfo = new RespInfo();
        respInfo.setCode(1);
        respInfo.setMsg("添加成功");
        respInfo.setData(a);
        return respInfo;
    }
    //删除
    @RequestMapping("deleteMajorById")
    public RespInfo deleteMajorById(Integer major_id) {
        int m = majorsService.deleteByPrimaryKey(major_id);
        System.out.println(m);
        RespInfo respInfo = new RespInfo();
        respInfo.setCode(1);
        respInfo.setMsg("删除成功");
        respInfo.setData(m);
        return respInfo;
    }
    //修改
    @RequestMapping(value = "updateMajor",method = RequestMethod.POST)
    public RespInfo updateMajor(@RequestBody Majors majors) {
        int u = majorsService.updateByPrimaryKey(majors);
        System.out.println(u);
        RespInfo respInfo = new RespInfo();
        respInfo.setCode(1);
        respInfo.setMsg("修改成功");
        respInfo.setData(u);
        return respInfo;
    }
}
